home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 11255 / 11255.xpi / chrome / content / controller / buttons / ButtonsHandler.js < prev    next >
Text File  |  2009-12-16  |  14KB  |  348 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * 
  3.  * Pearltrees add-on AMO, Copyright(C), 2009, Broceliand SAS, Paris, France 
  4.  * (company in charge of developing Pearltrees)
  5.  * 
  6.  * This file is part of ΓÇ£Pearltrees add-on AMOΓÇ¥.  
  7.  * 
  8.  * Pearltrees add-on AMO is free software: you can redistribute it and/or modify it under the 
  9.  * terms of the GNU General Public License version 3 as published by the Free Software Foundation.
  10.  * 
  11.  * Pearltrees add-on AMO is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
  12.  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
  13.  * See the GNU General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License along with Pearltrees add-on AMO. 
  16.  * If not, see <http://www.gnu.org/licenses/>
  17.  * 
  18.  * ***** END LICENSE BLOCK ***** */
  19.  
  20. // ///////////////////////////////////////////////////////////////////////////////
  21. // Buttons controller
  22. // ///////////////////////////////////////////////////////////////////////////////
  23.  
  24. var BRO_ButtonsHandler = {
  25.  
  26.     DEFAULT_BAR_WIDTH : 350,
  27.     MODE_BUTTON_MAX_CHAR : 7,
  28.     _recordingModeInitValue : null,
  29.     restoreDefaultPostionOnLoad:false,
  30.     
  31.     init : function(recordingMode) { 
  32.         // Use init param because BRO_toolbar.getRecordingMode() is wrong after
  33.         // a call with BRO_tools.callWithDelay... (weird)
  34.         this._recordingModeInitValue = recordingMode;
  35.         window.addEventListener("load", BRO_ButtonsHandler.onViewLoad, false);
  36.     },
  37.  
  38.     // @todo call this function when buttons come from the customize panel
  39.     onButtonsCreated : function() {
  40.         BRO_recordButtonController.fixButtonSize();
  41.  
  42.         // Button effects are using CSS. So we sent the src to null.
  43.         var recordButton = document.getElementById("BRO_recordButton");
  44.         var newButton = document.getElementById("BRO_newButton");
  45.         var homeButton = document.getElementById("BRO_homeButton");
  46.  
  47.         if (recordButton)
  48.             recordButton.image = null;
  49.         if (newButton) 
  50.             newButton.image = null;
  51.         if (homeButton) 
  52.             homeButton.image = null;
  53.  
  54.         // backup selected mode from preferences
  55.         BRO_recordButtonController.refreshModeSelection(this._recordingModeInitValue);
  56.  
  57.         BRO_inButtonController.initTreeList();
  58.     },
  59.     
  60.     addPanels: function() {        
  61.         if(BRO_toolbar.isBrowserVersionGreaterOrEqual("3.0")) {           
  62.             var mainWindow = document.getElementById('main-window');
  63.             
  64.             // Add createNewTree panel
  65.             var newTreePanel = document.createElement('panel');
  66.             newTreePanel.setAttribute('id', "BRO_newTreePanel");
  67.             newTreePanel.setAttribute('class', "BRO_menuPopup");
  68.             newTreePanel.setAttribute('onpopupshowing', "BRO_inButtonController.onNewTreePanelShowing();");
  69.             var newTreePanelContent = document.createElement('vbox');
  70.             newTreePanelContent.setAttribute('id', "BRO_newTreePanelContent");
  71.             newTreePanelContent.setAttribute('class', "BRO_nameMapPopupContent");
  72.             newTreePanelContent.setAttribute('onCancel', "BRO_inButtonController.onClickCancelNewTree()");
  73.             newTreePanelContent.setAttribute('onValidate', "BRO_inButtonController.onClickValidateNewTree()");
  74.             newTreePanel.appendChild(newTreePanelContent);
  75.             mainWindow.appendChild(newTreePanel);
  76.             
  77.             // Add note panel
  78.             var notePanel = document.createElement('panel');
  79.             notePanel.setAttribute('id', "BRO_notePanel");
  80.             notePanel.setAttribute('class', "BRO_menuPopup");
  81.             notePanel.setAttribute('onpopupshowing', "BRO_noteController.onPanelShowing();");
  82.             var notePanelContent = document.createElement('vbox');
  83.             notePanelContent.setAttribute('id', "BRO_notePanelContent");
  84.             //notePanelContent.setAttribute('defaultText', "kikou");
  85.             notePanelContent.setAttribute('class', "BRO_notePopupContent");
  86.             notePanelContent.setAttribute('onCancel', "BRO_noteController.onClickCancelNote()");
  87.             notePanelContent.setAttribute('onValidate', "BRO_noteController.onClickValidateNote()");
  88.             notePanel.appendChild(notePanelContent);
  89.             mainWindow.appendChild(notePanel);
  90.         }
  91.     },
  92.     
  93.     closeButtonPopups : function () {
  94.         BRO_inButtonController.closePopup();
  95.         BRO_recordButtonController.closePopup();
  96.         BRO_noteController.closePopup();        
  97.     },
  98.  
  99.     onViewLoad : function() {
  100.         var d = new Date();
  101.         var timeToLoad = d.getTime() - BRO_toolbar.initTime;
  102.         BRO_log.log("view loaded (" + timeToLoad + " ms)");
  103.         BRO_toolbar.viewLoaded = true;
  104.  
  105.         if (BRO_toolbar.isFirstInstall) {        
  106.             BRO_ButtonsHandler.addFirstInstallButtonInNavBar();
  107.             BRO_model.getTreesAndCurrentUser(skipNotificationIfNotLogged = true);
  108.         }
  109.         else if (BRO_toolbar.isUpdate) {
  110.             if(BRO_ButtonsHandler.restoreDefaultPostionOnLoad) {
  111.                 BRO_ButtonsHandler.restoreDefaultPositionInNavbar();
  112.                 BRO_ButtonsHandler.restoreDefaultPostionOnLoad = false;
  113.             }
  114.             BRO_model.getTreesAndCurrentUser(skipNotificationIfNotLogged = true);
  115.         }
  116.  
  117.         BRO_ButtonsHandler.onButtonsCreated();
  118.         BRO_ButtonsHandler.addPanels();
  119.  
  120.         var d = new Date();
  121.         var timeToLoad = d.getTime() - BRO_toolbar.initTime;
  122.         BRO_log.log("creation complete (" + timeToLoad + " ms)");
  123.     },
  124.  
  125.     startRecording : function() {
  126.         // Update record button
  127.         BRO_recordButtonController.refreshRecordButtonLabel(BRO_toolbar.isRecording);
  128.         BRO_buttonEffectHelper.setButtonClass('BRO_recordButton','BRO_recordButtonIsRecording');
  129.  
  130.         // Update new button
  131.         BRO_inButtonController.refreshTreeListTextHeader(isRecording = true);
  132.         BRO_inButtonController.refreshTreeListHeight();
  133.     },
  134.  
  135.     stopRecording : function() {
  136.         // Update record button
  137.         BRO_recordButtonController.refreshRecordButtonLabel(BRO_toolbar.isRecording);
  138.         BRO_buttonEffectHelper.stopIsRecordingEffect();
  139.  
  140.         // Update new button
  141.         BRO_inButtonController.refreshTreeListTextHeader(isRecording = false);
  142.         BRO_inButtonController.refreshTreeListHeight();
  143.     },
  144.  
  145.     startOrContinueHistory : function(forceStartNew) {
  146.         BRO_toolbar.alertedForManyPearls = false;
  147.  
  148.         // The current page can be recorded
  149.         if (!BRO_navListener.urlLoading && BRO_model.isValidUrl(BRO_browserManager.getSelectedBrowserUrl())) {
  150.             if (BRO_inButtonController.getSelectedTree() && !forceStartNew) {
  151.                 this.recordCurrentPage();
  152.             } else if (BRO_inButtonController.getSelectedHistory() && !forceStartNew) {
  153.                 this.recordCurrentPage(); 
  154.             } else {
  155.                 this.recordCurrentPage(recordInNewTree = true);
  156.             }
  157.             BRO_toolbar.setRecording(true);
  158.             // Stop recording immediately if the mode is "one by one"
  159.             if (BRO_toolbar.recordingMode == RECORING_MODE_ONE_BY_ONE) {
  160.                 BRO_tools.callWithDelay('BRO_toolbar.setRecording(false)', BRO_buttonEffectHelper.START_RECORDING_EFFECT_TIME);
  161.             }
  162.         }
  163.         // The current page can't be recorded, we start recording without saving
  164.         // the page
  165.         else {
  166.             if (BRO_inButtonController.getSelectedTree() && !forceStartNew) {
  167.                 BRO_model.continueHistory(BRO_inButtonController.getSelectedTree().treeID);
  168.             } else if (BRO_inButtonController.getSelectedHistory() && !forceStartNew) {
  169.                 BRO_model.continueHistory(null, BRO_inButtonController.getSelectedHistory().id);
  170.             } else {
  171.                 BRO_model.start(BRO_inButtonController._selectedNewHistory.name);
  172.             }
  173.             BRO_actionListener.clear();
  174.             // Start recording only if the mode is "continuous"
  175.             if (BRO_toolbar.recordingMode == RECORING_MODE_CONTINUOUS) {
  176.                 BRO_toolbar.setRecording(true);
  177.             }
  178.         }
  179.     },
  180.  
  181.     /**
  182.      * Try to record the current page
  183.      * 
  184.      * @param boolean isStartNew
  185.      */
  186.     recordCurrentPage : function(isStartNew) {
  187.         var url = BRO_browserManager.getSelectedBrowserUrl();
  188.         var title = BRO_tools.removeFirefoxNameFromTitle(window.document.title);
  189.         var method = BRO_METHOD_UNKNOWN;
  190.         var browserID = BRO_browserManager.getSelectedBrowserID();
  191.         var time = BRO_tools.getTime();
  192.         var treeID = (BRO_inButtonController.getSelectedTree()) ? BRO_inButtonController.getSelectedTree().treeID : null;
  193.         var historyID = (BRO_inButtonController.getSelectedHistory()) ? BRO_inButtonController.getSelectedHistory().id : null;
  194.         var newHistoryName = (BRO_inButtonController._selectedNewHistory) ? BRO_inButtonController._selectedNewHistory.name : null;
  195.  
  196.         // The current page can be recorded
  197.         if (!BRO_navListener.urlLoading && BRO_model.isValidUrl(url)) {
  198.             if (!BRO_toolbar.isCurrentTreeFull()) {
  199.                 BRO_model.add(url, title, method, browserID, time, isStartNew, treeID, newHistoryName, historyID);
  200.                 if(method != BRO_ButtonsHandler.BRO_METHOD_TAB_CREATED) {
  201.                     BRO_toolbar.addUrlRecorded(url);
  202.                 }
  203.                 BRO_actionListener.clear();
  204.             }
  205.         }
  206.         // The current page can't be recorded and recording options are used, we
  207.         // start recording without saving the page
  208.         else if (isStartNew || treeID || historyID) {
  209.             this.startOrContinueHistory();
  210.         }
  211.     },
  212.  
  213.     onClickToolbar : function() {
  214.         if (BRO_toolbar.helpStartupWindow) {
  215.             BRO_toolbar.helpStartupWindow.close();
  216.         }
  217.     },
  218.     
  219.     addFirstInstallButtonInNavBar:function() {
  220.         var navbar = document.getElementById('nav-bar');
  221.         // Show navbar
  222.         if (navbar) {
  223.             navbar.collapsed = false;
  224.         }
  225.         
  226.         this.appendButtonInNavbar("BRO_firstInstallButton", "urlbar-container");
  227.         
  228.         // Make sure other buttons are in the customize palette
  229.         this.removeButton("BRO_homeButton");
  230.         this.removeButton("BRO_newButton");
  231.         this.removeButton("BRO_recordButton");
  232.     },
  233.     
  234.     restoreDefaultPositionInNavbar : function() {
  235.         // First restore default recording mode
  236.         BRO_toolbar.setRecordingMode(DEFAULT_RECORDING_MODE);
  237.  
  238.         var navbar = document.getElementById('nav-bar');
  239.         // Show navbar
  240.         if (navbar) {
  241.             navbar.collapsed = false;
  242.         }
  243.         
  244.         // Remove the firstInstall button
  245.         this.removeButton("BRO_firstInstallButton");
  246.         
  247.         // Install buttons
  248.         this.appendButtonInNavbar("BRO_homeButton", "urlbar-container");
  249.         this.appendButtonInNavbar("BRO_newButton", "BRO_homeButton");
  250.         this.appendButtonInNavbar("BRO_recordButton", "BRO_newButton");
  251.     },
  252.     
  253.     removeAllButtons:function() {
  254.         this.removeButton("BRO_firstInstallButton");
  255.         this.removeButton("BRO_homeButton");
  256.         this.removeButton("BRO_newButton");
  257.         this.removeButton("BRO_recordButton");    
  258.     },
  259.     
  260.     removeButton : function(buttonId) {
  261.         var palette = document.getElementById("navigator-toolbox").palette;        
  262.         
  263.         var button = document.getElementById(buttonId);
  264.         if(!button) return;
  265.         var parentBar = button.parentNode;
  266.         
  267.         while(button && parentBar) {
  268.         
  269.             if(parentBar) {            
  270.                 this.removeButtonFromToolbarCurrentSet(parentBar, buttonId);
  271.             }
  272.             
  273.             // Move item to the toolbar palette
  274.             if (palette) {
  275.                 // @todo check the item is not already in the palette            
  276.                 palette.appendChild(button);
  277.             } else {                 
  278.                 parentBar.removeChild(button);
  279.             }
  280.             BRO_log.log("Button removed: "+buttonId);
  281.             
  282.             // If the button has been duplicated we remove other instances
  283.             button = document.getElementById(buttonId);
  284.             if(button) {
  285.                 parentBar = button.parentNode;
  286.             }
  287.         }
  288.     },    
  289.  
  290.     appendButtonInNavbar : function(buttonId, beforeElementId) {
  291.         var toolbar = document.getElementById("nav-bar");
  292.         var button = document.getElementById(buttonId);
  293.         var beforeElement = document.getElementById(beforeElementId);
  294.         
  295.         // Remove button if exist
  296.         if (button) {            
  297.             this.removeButton(buttonId);            
  298.         }
  299.         
  300.         // Insert the button at the right position
  301.         toolbar.insertItem(buttonId, beforeElement);
  302.         this.appendButtonInToolbarCurrentSet(toolbar, buttonId, beforeElementId);
  303.         
  304.         BRO_log.log("Button added: "+buttonId);
  305.     },
  306.     
  307.     appendButtonInToolbarCurrentSet : function(toolbar, buttonId, beforeElementId) {
  308.         var oldset = toolbar.getAttribute("currentset");    
  309.         if(!oldset) {            
  310.             oldset = toolbar.getAttribute("defaultset");        
  311.         }
  312.         if(oldset.indexOf(buttonId) != -1) {
  313.             this.removeButtonFromToolbarCurrentSet(toolbar, buttonId);
  314.         }
  315.         var newset = "";
  316.         if(!beforeElementId || oldset.indexOf(beforeElementId) == -1) {
  317.             if (oldset && oldset != "") {
  318.                 newset = oldset + ",";
  319.             }
  320.             newset += buttonId;
  321.         }
  322.         else {
  323.             var beforeElementIndex = oldset.indexOf(beforeElementId);
  324.             var setBefore = oldset.substring(0, beforeElementIndex);
  325.             var setAfter = oldset.substring(beforeElementIndex, oldset.length);
  326.             newset = setBefore + buttonId + "," + setAfter;
  327.         }
  328.  
  329.         toolbar.setAttribute("currentset", newset);
  330.         document.persist(toolbar.id, "currentset");
  331.         return newset;
  332.     },
  333.  
  334.     removeButtonFromToolbarCurrentSet : function(toolbar, buttonId) {
  335.         var oldset = toolbar.getAttribute("currentset");
  336.         if (!oldset || oldset == "" || oldset.indexOf(buttonId) == -1)
  337.             return oldset;
  338.         var reg = new RegExp(buttonId + ",?", "gi");
  339.         var newset = oldset.replace(reg, "");
  340.         if (newset.charAt(newset.length - 1) == ",") {
  341.             newset = newset.substring(0, newset.length - 1);
  342.         }
  343.  
  344.         toolbar.setAttribute("currentset", newset);
  345.         document.persist(toolbar.id, "currentset");
  346.         return newset;
  347.     }
  348. }